package com.micro.ai.models.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.ai.commons.domain.ApiResponse;
import com.micro.ai.models.entity.DatasetRecord;
import com.micro.ai.models.service.DatasetRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 数据集记录控制器
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/api/dataset-records")
@Tag(name = "数据集记录管理", description = "数据集记录的增删改查")
public class DatasetRecordController {

    @Autowired
    private DatasetRecordService recordService;

    @PostMapping
    @PreAuthorize("hasRole('ADMIN') or hasAuthority('dataset:record:create')")
    @Operation(summary = "创建数据集记录", description = "创建新的数据集记录")
    public ApiResponse<DatasetRecord> create(@Valid @RequestBody DatasetRecord record) {
        log.info("创建数据集记录请求: datasetId={}, index={}", record.getDatasetId(), record.getRecordIndex());
        DatasetRecord result = recordService.create(record);
        return ApiResponse.success("创建成功", result);
    }

    @PostMapping("/batch")
    @PreAuthorize("hasRole('ADMIN') or hasAuthority('dataset:record:create')")
    @Operation(summary = "批量创建数据集记录", description = "批量创建数据集记录")
    public ApiResponse<List<DatasetRecord>> batchCreate(
            @Parameter(description = "数据集ID", required = true) @RequestParam String datasetId,
            @Valid @RequestBody List<DatasetRecord> records) {
        log.info("批量创建数据集记录请求: datasetId={}, count={}", datasetId, records.size());
        // created_by 由后端服务层或数据库触发器填充
        List<DatasetRecord> result = recordService.batchCreate(records, datasetId);
        return ApiResponse.success("批量创建成功", result);
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN') or hasAuthority('dataset:record:update')")
    @Operation(summary = "更新数据集记录", description = "更新数据集记录信息")
    public ApiResponse<DatasetRecord> update(
            @Parameter(description = "记录ID", required = true) @PathVariable String id,
            @Valid @RequestBody DatasetRecord record) {
        log.info("更新数据集记录请求: recordId={}", id);
        
        DatasetRecord result = recordService.update(id, record);
        return ApiResponse.success("更新成功", result);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN') or hasAuthority('dataset:record:delete')")
    @Operation(summary = "删除数据集记录", description = "删除数据集记录")
    public ApiResponse<Void> delete(
            @Parameter(description = "记录ID", required = true) @PathVariable String id) {
        log.info("删除数据集记录请求: recordId={}", id);
        
        recordService.delete(id);
        return ApiResponse.successVoid("删除成功");
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN') or hasRole('USER') or hasAuthority('dataset:record:view')")
    @Operation(summary = "获取记录详情", description = "根据ID获取记录详细信息")
    public ApiResponse<DatasetRecord> getById(
            @Parameter(description = "记录ID", required = true) @PathVariable String id) {
        log.info("查询数据集记录详情: recordId={}", id);
        
        DatasetRecord record = recordService.getById(id);
        if (record == null) {
            return ApiResponse.error("404", "记录不存在");
        }
        
        return ApiResponse.success(record);
    }

    @GetMapping("/by-dataset/{datasetId}")
    @PreAuthorize("hasRole('ADMIN') or hasRole('USER') or hasAuthority('dataset:record:view')")
    @Operation(summary = "分页查询数据集记录", description = "根据数据集ID分页查询记录列表")
    public ApiResponse<Page<DatasetRecord>> pageQuery(
            @Parameter(description = "数据集ID", required = true) @PathVariable String datasetId,
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "20") @RequestParam(defaultValue = "20") int pageSize,
            @Parameter(description = "租户ID") @RequestParam(required = false) String tenantId) {
        log.info("查询数据集记录: datasetId={}, pageNum={}, pageSize={}", datasetId, pageNum, pageSize);
        
        Page<DatasetRecord> page = recordService.pageQuery(pageNum, pageSize, tenantId, datasetId);
        return ApiResponse.success(page);
    }

}

